package at.fhooe.mc.master.clc.client.presenter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import at.fhooe.mc.master.clc.client.MetricsServiceAsync;
import at.fhooe.mc.master.clc.client.MetricsVisualizationEventBus;
import at.fhooe.mc.master.clc.client.view.MetricsRequestView;
import at.fhooe.mc.master.clc.client.widget.CheckBoxTable;
import at.fhooe.mc.master.clc.client.widget.MessageBox;
import at.fhooe.mc.master.clc.shared.domain.FrameworkID;
import at.fhooe.mc.master.clc.shared.domain.metrics.IMetricsResponse;
import at.fhooe.mc.master.clc.shared.domain.metrics.MetricKey;
import at.fhooe.mc.master.clc.shared.domain.metrics.MetricsRequest;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

@Presenter(view = MetricsRequestView.class)
public class MetricsRequestPresenter extends BasePresenter<MetricsRequestPresenter.IMetricsRequestView, MetricsVisualizationEventBus>
{
	public interface IMetricsRequestView extends IsWidget
	{
		public void setFrameworkListWidget(IsWidget widget);
		
		public void setMetricListWidget(IsWidget widget);
		
		
		public Button getCreateGraphButton();
		
		public Set<Integer> getSelectedFrameworkIndexes();
		
		public Set<Integer> getSelectedMetricsIndexes();
		
		public Date getStartDate();
		
		public Date getEndDate();

		public DisclosurePanel getCollapsiblePanel();
		
	}
	
	@Inject 
	private MetricsServiceAsync service;
	
	public void onStart()
	{
		CheckBoxTable frameworkList = new CheckBoxTable();
		for (FrameworkID fid : FrameworkID.values())
		{
			frameworkList.addClickedRow(true, fid.getLabel());
		}
		view.setFrameworkListWidget(frameworkList);
		
		CheckBoxTable metricsTable = new CheckBoxTable();
		for (MetricKey mkey : MetricKey.values())
		{
			if (mkey.isActive())
			{
				metricsTable.addRow(mkey.getLabel());
			}
			else
			{	
				// add not-enabled row for those entries inactive
				metricsTable.addRow(false, mkey.getLabel());
			}
		}
		view.setMetricListWidget(metricsTable);
		eventBus.setMetricsRequestWidget(view);
	}
	
	@Override
	public void bind()
	{
		Button requestGraph = view.getCreateGraphButton();
		requestGraph.addClickHandler(new CreateGraphHandler());
	}
	
	private void requestMetrics(MetricsRequest request)
	{
		service.getMetricsGraphs(request, new AsyncCallback<IMetricsResponse>()
		{
			@Override
			public void onSuccess(IMetricsResponse result)
			{
				if (result == null || result.getMetricsDataTables() == null || result.getMetricsDataTables().isEmpty())
				{
					System.out.println("Result empty...");
					view.getCreateGraphButton().setEnabled(true);
					return;
				}
				
				for (MetricKey mKey : result.getMetricsDataTables().keySet())
				{
					if (result.getMetricsDataTables().get(mKey) == null)
					{
						System.out.println("Error in metrics data loading.");
						view.getCreateGraphButton().setEnabled(true);
						return;
					}
					
					eventBus.createGraph(SimpleLineGraphPresenter.class, 
							result.getMetricsDataTables().get(mKey).get("data"), 
							result.getMetricsDataTables().get(mKey).get("vAxisTitle"), 
							result.getMetricsDataTables().get(mKey).get("graphTitle"));
				}
				view.getCreateGraphButton().setEnabled(true);
			}
			
			@Override
			public void onFailure(Throwable caught)
			{
				view.getCreateGraphButton().setEnabled(true);
				System.out.println("Error in Metrics Request! Something went wrong on server...");
			}
		});
	}
	
	
	class CreateGraphHandler implements ClickHandler
	{
		@Override
		public void onClick(ClickEvent event)
		{
			view.getCreateGraphButton().setEnabled(false);
			
			List<FrameworkID> requestFrameworks = new ArrayList<FrameworkID>();
			for (int i : view.getSelectedFrameworkIndexes())
			{
				requestFrameworks.add(FrameworkID.values()[i]);
			}
			
			if (requestFrameworks.isEmpty())
			{
				MessageBox box = new MessageBox("Error, cannot request data", "No framework selected for loading data.");
				box.center();
				view.getCreateGraphButton().setEnabled(true);
				return;
			}
			
			List<MetricKey> requestMetrics = new ArrayList<MetricKey>();
			for (int i : view.getSelectedMetricsIndexes())
			{
				requestMetrics.add(MetricKey.values()[i]);
			}
			
			if (requestFrameworks.isEmpty())
			{
				MessageBox box = new MessageBox("Error, cannot request data", "No metrics keys selected for loading.");
				box.center();
				view.getCreateGraphButton().setEnabled(true);
				return;
			}
			
			Date requestStartDate = view.getStartDate();
			Date requestEndDate = view.getEndDate();
			
			if (requestStartDate == null || requestEndDate == null)
			{
				MessageBox box = new MessageBox("Error, cannot request data", "Start Date or End Date was not set correctly!");
				box.center();
				view.getCreateGraphButton().setEnabled(true);
				return;
			}
			
			MetricsRequest request = new MetricsRequest(requestStartDate, requestEndDate, requestFrameworks, requestMetrics);
			
			requestMetrics(request);
		}
	}
}
